//
//  PrivacyAgreement.swift
//  taxiDriver
//
//  Created by 小哥哥 on 2023/12/9.
//

import SwiftUI
import AlertToast

struct PrivacyAgreement: View {
    
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    var vm = ViewModel.shared
    @State var privacyAgreementString = ""
    @State var contentHeight = 0.0
    @State var show = false
    @State var showFail = false
    
    var body: some View {
        NavigationView {
            GeometryReader { geometry in
                ScrollView(showsIndicators: false) {
                    Text(privacyAgreementString)
                        .frame(width: geometry.size.width - 10,height: contentHeight)
                        .font(.system(size: 16))
                        .padding(10) //方法2
                }
                .clipped()
            }
            
        }  .navigationBarHidden(false)
            .navigationBarBackButtonHidden(true) // 隐藏默认返回按钮
            .navigationBarItems(leading: backButton) // 添加自定义返回按钮
            .navigationTitle("隐私政策")
//            .statusBarColor(.init(hex: 0x85308D), "隐私政策")
            .onAppear() {
                self.show = true
                getPrivacyAgreementRequest()
            }
            .toast(isPresenting: $show, alert: {
                
                return AlertToast(type: .loading, title: "")
            })
        
            .toast(isPresenting: $showFail, alert: {
                
                return AlertToast(type: .error(.red), title: String(localized: "隐私政策获取失败"))
            })
        
    }
    var backButton: some View {
        Button(action: {
            presentationMode.wrappedValue.dismiss()
        }) {
            HStack {
                Image(systemName: "chevron.backward").foregroundColor(Color(hex: 0x333333))
            }
        }
    }
    
    func getPrivacyAgreementRequest() {
        
        let lang = ViewModel.shared.language.toString()
        let openId = "driver"
     
    }
}

extension String {
    
    func getHeight(width: CGFloat, font: UIFont, lineSpacing: CGFloat = 0) -> CGFloat {
        
        let attStr = NSMutableAttributedString(string: self)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        attStr.addAttributes([NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : paragraphStyle], range: NSRange(location: 0, length: attStr.length))
        let height = attStr.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin], context: nil).size.height
        
        return ceil(height)
    }
    
    func getWidth(height: CGFloat, font: UIFont, lineSpacing: CGFloat = 0) -> CGFloat {
        
        let attStr = NSMutableAttributedString(string: self)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        attStr.addAttributes([NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : paragraphStyle], range: NSRange(location: 0, length: attStr.length))
        let width = attStr.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: [.usesLineFragmentOrigin], context: nil).size.width
        
        return ceil(width)
    }
}

//#Preview {
//    PrivacyAgreement()
//}
